package com.scau.hwadee.examination.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

/**
 * @author: YHX
 * @Date: 2019/7/4 14:08
 * 生成验证码图片
 */
public class AuthCodeUtils {

    /**
     * 随机数字符串
     */
    private static String sRand;

    /**
     * 验证码位数
     *
     */
    private static final int SRAND_BITS=4;

    public static String getsRand() {
        return sRand;
    }

    /**
     * 颜色的最大值
     */
    private static final int COLOR_MAX = 255;

    /**
     *
     * @param fc
     * @param bc
     * @return
     */
    private static final int INTERFERENCE_LINE =300;
    private static Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
        Random random = new Random();
        if (fc > COLOR_MAX) {
            fc = COLOR_MAX;
        }
        if (bc > COLOR_MAX) {
            bc = COLOR_MAX;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    public static BufferedImage createImage() {
        int width = 109;
        int height = 40;
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);

        /**
         * 获取图形上下
         */
        Graphics g = image.getGraphics();

        /**
         * 生成随机
         */
        Random random = new Random();

        /**
         * 设定背景
         */
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);

        /**
         * 设定字体
         */
        g.setFont(new Font("Times New Roman", Font.ITALIC, 40));

        /**
         * 画边
         */
        g.drawRect(0, 0, width - 1, height - 1);
        /**
         * 随机产生155条干扰线，使图象中的认证码不易被其它程序探测
         */
        g.setColor(getRandColor(160, 200));
        for (int i = 0; i < INTERFERENCE_LINE; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }

        /**
         * 取随机产生的认证??(4位数)
         * 清空原有字符
         */
        sRand = "";
        for (int i = 0; i < SRAND_BITS; i++) {
            String rand = String.valueOf(random.nextInt(10));
            sRand += rand;
            /**
             * 将认证码显示到图象中
             * 调用函数出来的颜色相同，可能是因为种子太接近，所以只能直接生
             */
            g.setColor(new Color(20 + random.nextInt(110), 20 + random
                    .nextInt(110), 20 + random.nextInt(110)));
            g.drawString(rand, 25 * i + 6, 34);
        }
        /**
         * 图象生效
         */
        g.dispose();
        return image;
    }

    public static void main(String[] args) {
        try {
            ImageIO.write(AuthCodeUtils.createImage(), "png", new File("F:\\test.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}